package com.dalongxin.work.page;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.dalongxin.work.Constants;
import com.dalongxin.work.R;
import com.dalongxin.work.data.Department;
import com.dalongxin.work.page.adapter.DepartmentAdapter;
import com.dalongxin.work.utils.HttpUtil;
import com.dalongxin.work.utils.ItemClickSupport;
import com.dalongxin.work.utils.StatusBarUtil;
import com.dalongxin.work.utils.ToastUtil;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通讯录列表（部门）
 */
@ContentView(R.layout.activity_common_list)
public class DepartmentListActivity extends BaseActivity {

    @ViewInject(R.id.tool_bar)
    private Toolbar toolbar;
    @ViewInject(R.id.title_text)
    private TextView titleText;
    @ViewInject(R.id.recycler_view)
    private RecyclerView recyclerView;
    @ViewInject(R.id.xrefreshview)
    private XRefreshView xRefreshView;

    @ViewInject(R.id.image_no_data)
    private View mNoDataImg;
    @ViewInject(R.id.normal_tips_view)
    private TextView mNormalTipsView;
    @ViewInject(R.id.special_layout)
    private LinearLayout mSpecialLayout;
    @ViewInject(R.id.first_tips)
    private TextView mFirstTipsView;
    @ViewInject(R.id.second_tips)
    private TextView mSecondTipsView;

    private List<Department> mData;
    private DepartmentAdapter mAdapter;

    public static final String DATA_CHOOSE_DEPARTMENT = "data_choose_department";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        this.setSupportActionBar(toolbar);//这样menu才能显示出来
        getSupportActionBar().setDisplayShowTitleEnabled(false);//去掉默认title的显示
        StatusBarUtil.setPaddingSmart(this, toolbar);
        StatusBarUtil.darkMode(this, false);
        titleText.setText("选择部门");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initEmptyView();

        mData = new ArrayList<>();
        mAdapter = new DepartmentAdapter(mData);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        // 静默加载模式不能设置footerview
        recyclerView.setAdapter(mAdapter);
        //设置刷新完成以后，headerview固定的时间
        //xRefreshView.setPinnedTime(1000);
        xRefreshView.setMoveForHorizontal(true);
        //xRefreshView1.setAutoLoadMore(true);
        mAdapter.setCustomLoadMoreView(new XRefreshViewFooter(this));
        //设置静默加载时提前加载的item个数
        //xRefreshView1.setPreLoadCount(4);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                getDataList();
            }
            @Override
            public void onLoadMore(boolean isSilence) {
            }
        });
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, View itemView, int position) {
                Department item = mData.get(position);
                Intent intent = new Intent();
                intent.putExtra(DATA_CHOOSE_DEPARTMENT, item);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        xRefreshView.setPullRefreshEnable(true);//允许下拉刷新
        xRefreshView.setPullLoadEnable(false);//禁止加载更多
        xRefreshView.setEmptyView(mNoDataImg);
        xRefreshView.setAutoRefresh(true);
    }

    private void initEmptyView() {
        mNormalTipsView.setVisibility(View.GONE);
        mSpecialLayout.setVisibility(View.VISIBLE);
        mFirstTipsView.setText(getString(R.string.class_cloud_disk_empty_first_tips));
        mSecondTipsView.setText(getString(R.string.class_cloud_disk_empty_second_tips));
    }

    private void getDataList() {
        Map<String, String> params = new HashMap<>();
        HttpUtil.getInstance().post(true, Constants.url_contactList, params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                LogUtil.e("response=" + response);
                if (response.optInt("ret") == 0) {
                    List<Department> temp = Department.parseFromJsonListInDepartment(response.optJSONArray("data"));
                    if(temp != null) {
                        mData.clear();
                        mData.addAll(temp);
                        spUtils.putListData(Constants.Key.SP_DEPARTMENT, mData);
                    }
                    mAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showShort(response.optString("msg"));
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                xRefreshView.stopRefresh();
            }
        });
    }
}
